* rifregtable.do	JEP		01/26/15
* Builds regression tables for third year paper version, updated from rifregtable.do
* This version does horizontal style tables, like Manacorda

*************************** Change log ********************************** 

/*
	
*/	

cap program drop getfe
program define getfe, rclass
	* City Industry FE: All
	loc cxis="Yes"
	* City Employment: All
	loc cityemps="Yes"
	* Bartik price
	if strmatch("`1'","*bp*") loc bps="Yes"
	else loc bps=""
	* Bartik Quantity
	*if strmatch("`1'","*bq*") loc bqs="Yes"
	*else loc bqs=""
	* City Trends
	if strmatch("`1'","*sst*")  loc csts="Yes" 
	else if strmatch("`1'","*ct*")  loc csts="Yes" 
	else loc csts=""
	* Industry employment
	if strmatch("`1'","*ie*") loc ies="Yes"
	else loc ies=""
	* Industry trends
	if strmatch("`1'","*itct*") loc its="Yes"
	else loc it=""
	return local cxi="`cxis'"
	return local tfe="Yes"
	return local cityemp="`cityemps'"
	return local bp="`bps'"
	return local cst="`csts'"
	return local ie="`ies'"	
	return local it="`its'"
	*return local bq="`bqs'"
	
	
end




cap program drop etable
program etable
	syntax, nrifs(string) time(string) panels(string) vars(string) master(string) outdir(string) mw(string) per(string) [dom(string) new(string) wrap makepdf caption(string) register]  

	use "`master'/Data/Source/ocupados_mw.dta", clear
	* Find different panels from file names
	* For robustness checks, only the two last panels
	glo head "Dep. var. (log)"
	glo botn ""
	glo botn2 ""
	* glo botr ""
	glo botcxi ""
	glo bottfe ""
	glo botcityemp ""
	glo botbp ""
	glo botcst ""
	glo botie ""
	glo botit ""
	*glo botcfe ""
	* glo botbq ""
	
	* Get first and last var
	loc fw: word 1 of `vars'
	loc count: word count `vars'
	loc lw: word `count' of `vars'	
	
	foreach j in `vars' {
		
		if "`j'"=="d_ind_ind" glo l`j' "Industry unemp."
		if "`j'"=="d_ind_c" glo l`j' "City unemp. ind. shr."
		if "`j'"=="e_odi_ind_c" glo l`j' "City emp. ind. shr."
		if "`j'"=="horas" glo l`j' "Hours worked"
		if "`j'"=="o_ind" glo l`j' "Employment"
	
		glo cols ""
		glo betas ""
		glo ses ""
		glo ps ""
		glo colnum=1
		loc i=1
		foreach pnl in `panels' {
			loc nrif : word `i' of `nrifs'
			glo cols "${cols}c"
			* Header - Column number
			if "`j'"=="`fw'" {
				glo head "${head} & (${colnum})"
			}
			* Centered columns
			if "`j'"=="horas" loc ff "h"
			else loc ff "e"
			est use "`master'/Ster/`ff'`nrif'`new'/reg_`pnl'_tq(`time')_`mw'_`j'_50`dom'.ster"
			* if "`register'"=="register" project, uses("`master'/Ster/`ff'`nrif'`new'/reg_`pnl'_tq(`time')_`mw'_`j'_50`dom'.ster")
			est replay
			* di e(N)
			glo b:  di %10.4f _b[treat]
			glo se :  di %10.4f _se[treat]
			glo p=e(tp)
			if $p<0.001 glo star "***"
			else if $p<0.05 glo star "**"
			else if $p<0.1 glo star "*"
			else glo star ""
			glo p:  di %10.3f $p
			glo n= e(N)
			noi di "Observaciones = $n"
			if "`j'"!="`lw'" glo n2 = e(N)
			* glo r = e(r2)
			* glo r: di %9.3f $r
			glo betas "${betas} & ${b}${star}"
			glo ses "${ses} & ($se )"
			glo ps "${ps} & [$p ]"	
						
			glo colnum = ${colnum}+1	
			
			if "`j'"=="`lw'" {
				* Bottom 1 - N
				glo botn "${botn} & $n"
				glo botn2 "${botn2} & $n2"
				glo botr "${botr} & $r"
				getfe `pnl'
				glo botcxi "${botcxi} & `r(cxi)'"
				glo bottfe "${bottfe} & `r(tfe)'"
				glo botcityemp "${botcityemp} & `r(cityemp)'"
				glo botbp "${botbp} & `r(bp)'"
				glo botcst "${botcst} & `r(cst)'"
				glo botie "${botie} & `r(ie)'"
				glo botit "${botit} & `r(it)'"
			}
		
		loc i=`i'+1
		
		}
		if "`j'"=="`fw'"  loc r "replace" 
		else loc r "append"	

		* cd "C:/Users/jorge_perez/Desktop/AA_Minimum_Wage/Doc/reg"
		texdoc init `outdir'/`nrif'_`time'.tex, `r' force
		if "`j'"=="`fw'" {
			if "`wrap'"=="wrap" {
				tex \documentclass{article}
				tex \usepackage[letterpaper,left=3cm,right=3cm,top=3cm,bottom=3cm]{geometry}
				* tex \usepackage{adjustbox}
				tex \begin{document}
				tex \begin{table}
				tex \caption{`caption'}
				tex \centering
				tex \footnotesize
				* tex \begin{adjustbox}{center}
			}
			tex \begin{tabular}{l|lll|lll} \hline
		}
		if "`j'"=="`fw'" {
			tex & \multicolumn{3}{c|}{Formal} & \multicolumn{3}{c}{Informal} \\\hline
			tex $head \\\hline
		}
		tex ${l`j'} $betas \\
		tex $ses \\
		tex $ps \\	
		if "`j'"=="`lw'" {
			tex \\\hline
			tex City - industry Cells $botn2 \\
			tex Observations $botn \\
			* tex R squared $botr \\
			tex City x Industry FE $botcxi \\
			* tex City FE $botcfe \\
			tex Time FE $bottfe \\
			tex Bartik Price Shocks $botbp \\
			* tex Bartik quantity shocks $botbq \\
			tex City Specific Trends  $botcst \\
			tex Industry Trends $botit \\
			
		}
		texdoc close		
	}
	texdoc init `outdir'/`nrif'_`time'.tex, append force
	tex \hline
	tex \end{tabular}
	if "`wrap'"=="wrap"{ 
		* tex \end{adjustbox
		tex \end{table}
		tex \end{document}
	}
	texdoc close
	
	if "`makepdf'"=="makepdf" {
		cd "`outdir'"
		shell pdflatex `outdir'/`nrif'_`time'.tex
		! `outdir'/`nrif'_`time'.pdf
	}

end

